package com.dubito.gallery.ui.settings

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SmallTopAppBar
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.dubito.gallery.LocalNavController
import com.dubito.gallery.R
import com.google.accompanist.systemuicontroller.rememberSystemUiController

@Composable
fun SettingsFolderScreen(viewModel: SettingsFolderViewModel = viewModel()) {
    val uiState by viewModel.uiState.collectAsState()
    SettingsFolderContent(
        uiState = uiState,
        onChangeFolderVisibility = { folderId, visibility ->
            viewModel.setFolderVisibility(id = folderId, visibility = visibility)
        }
    )
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsFolderContent(
    uiState: SettingsFolderUiState,
    onChangeFolderVisibility: (Int, Boolean) -> Unit
) {
    val topAppBarState = rememberTopAppBarState()
    val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(topAppBarState)

    val scrollFraction by remember {
        derivedStateOf {
            topAppBarState.overlappedFraction
        }
    }
    val statusBarColor = TopAppBarDefaults.centerAlignedTopAppBarColors()
        .containerColor(colorTransitionFraction = scrollFraction).value
    val systemUiController = rememberSystemUiController()
    SideEffect {
        systemUiController.setStatusBarColor(statusBarColor)
    }
    Scaffold(
        modifier = Modifier
            .nestedScroll(scrollBehavior.nestedScrollConnection)
            .safeDrawingPadding(),
        topBar = {
            val navController = LocalNavController.current
            SmallTopAppBar(
                title = { Text(text = stringResource(id = R.string.setting_title_folder)) },
                scrollBehavior = scrollBehavior,
                navigationIcon = {
                    IconButton(onClick = { navController.navigateUp() }) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = stringResource(id = R.string.desc_back)
                        )
                    }
                }
            )
        }
    ) { innerPadding ->
        LazyColumn(contentPadding = innerPadding) {
            if (uiState is SettingsFolderUiState.Loaded) {
                item { SettingsFolderDescription() }
                items(uiState.settingFolderItems) { folder ->
                    SettingsFolderCell(
                        folder = folder
                    ) { checked ->
                        onChangeFolderVisibility(folder.id, checked)
                    }
                }
            }
        }
    }
}

@Composable
fun SettingsFolderDescription() {
    Text(
        text = stringResource(id = R.string.setting_desc_folder),
        style = MaterialTheme.typography.bodyMedium,
        color = MaterialTheme.colorScheme.outline,
        modifier = Modifier.padding(
            horizontal = SettingsScreenTokens.ItemPaddingStart,
            vertical = SettingsScreenTokens.ItemPaddingVertical
        )
    )
}

@Composable
fun SettingsFolderCell(folder: SettingsFolderItem, onCheckedChange: (Boolean) -> Unit) {
    Column {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .fillMaxWidth()
                .padding(
                    horizontal = SettingsScreenTokens.ItemPaddingStart,
                    vertical = SettingsScreenTokens.ItemPaddingVertical
                )
        ) {
            AsyncImage(
                model = folder.thumbnailUri,
                contentDescription = "Thumbnail of the folder",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .background(color = MaterialTheme.colorScheme.surfaceVariant)
                    .size(80.dp)
            )
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 20.dp)
            ) {
                Text(text = folder.name, style = MaterialTheme.typography.titleMedium)
                Text(
                    text = folder.parentPath,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.outline
                )
            }
            Checkbox(checked = folder.visibility, onCheckedChange = onCheckedChange)
        }
    }
    Divider(color = MaterialTheme.colorScheme.outline, thickness = Dp.Hairline)
}